package com.xcx.baseManger.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xcx.baseManger.helper.PermissionHelper;
import com.xcx.baseManger.service.AclUserService;
import com.xcx.commoncore.entity.aclentity.AclPermission;
import com.xcx.baseManger.mapper.AclPermissionMapper;
import com.xcx.baseManger.service.AclPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcx.commoncore.entity.aclentity.AclUser;
import com.xcx.commonuntils.returnuntls.ReturnMessage;
import com.xcx.commonuntils.returnuntls.ReturnMessageUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.security.acl.Permission;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-10-23
 */


@Service
@Primary
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AclPermissionServiceImpl extends ServiceImpl<AclPermissionMapper, AclPermission> implements AclPermissionService {

    final private AclPermissionMapper aclPermissionMapper;



    @Override
    public List<AclPermission> getPermissionByOpenid(AclUser aclUser) {


        List<AclPermission> selectPermissionList = null;
        if (this.isSysAdmin(aclUser.getUsername())) {
            //如果是超级管理员，获取所有菜单
            selectPermissionList = aclPermissionMapper.selectList(null);
        } else {


            selectPermissionList = aclPermissionMapper.selectPermissionByUserId(aclUser.getId());
        }

        List<AclPermission> permissionList = PermissionHelper.bulid(selectPermissionList);
//        List<JSONObject> result = MemuHelper.bulid(permissionList);

        List<AclPermission> newPermissionList = permissionList.stream().distinct().collect(Collectors.toList());
        return newPermissionList;
    }


    /**
     * 判断用户是否系统管理员
     * @param userByUserName
     * @return
     */
    private boolean isSysAdmin(String userByUserName) {

        if ("admin".equals(userByUserName)) {
            return true;
        }
        return false;
    }






}
